void init_ultrasonic(void)
{
  pinMode( US_ALT_rx, OUTPUT );
  digitalWrite( US_ALT_rx, HIGH );
} 
       /**************************************************************
    * 
    ***************************************************************/
unsigned int raw_ultrasonic(void) //Return the altitude exactly at the moment you request it... 
{
  unsigned int pulse_length=0; //declaring variable
  static float average_pulse = 0;
  average_pulse=(((float)average_pulse*.50)+(float)((float)analogRead(2)*.50));
  pulse_length=average_pulse;
  return pulse_length;
}
         /**************************************************************
    * 
    ***************************************************************/
unsigned int pulse_ultrasonic(unsigned int refresh_rate)//Returns the altitude with the specified refresh rate in milliseconds
{
  if((millis() - timer_ultrasonic) > refresh_rate)
  {
    altitude=raw_ultrasonic();
    timer_ultrasonic=millis();
  }  
  return altitude;
}  

unsigned int raw_forward_us( )
{
  unsigned int pulse_length;
  pinMode(US_FRONT, OUTPUT);
  digitalWrite(US_FRONT, LOW);
  delayMicroseconds(2);
  digitalWrite(US_FRONT, HIGH);
  delayMicroseconds(3);
  digitalWrite(US_FRONT, LOW);
  pinMode(US_FRONT, INPUT);
  pulse_length = pulseIn(US_FRONT, HIGH);
  // TODO: consider shifting left 6 bits instead
  return pulse_length>>6;
}

unsigned int pulseForwardUS( unsigned int refresh_rate, boolean * readForward )
{
  static unsigned int forwardDistance = 0;
  static unsigned int then = 0;
  unsigned int now = millis( );
  *readForward = false;
  
  if( forwardDistance == 0 || ( now - then ) > refresh_rate )
  {
    forwardDistance = raw_forward_us( );
    then = now;
    *readForward = true;
  }
  
  return forwardDistance;
}

unsigned int raw_starboard_us( )
{
  unsigned int pulse_length;
  pinMode(US_LEFT, OUTPUT);
  digitalWrite(US_LEFT, LOW);
  delayMicroseconds(2);
  digitalWrite(US_LEFT, HIGH);
  delayMicroseconds(3);
  digitalWrite(US_LEFT, LOW);
  pinMode(US_LEFT, INPUT);
  pulse_length = pulseIn(US_LEFT, HIGH);
  // TODO: consider shifting left 6 bits instead
  return pulse_length>>6;
}

unsigned int pulseStarboardUS( unsigned int refresh_rate, boolean * readStarboard )
{
  static unsigned int starboardDistance = 0;
  static unsigned int then;
  unsigned int now = millis( );
  *readStarboard = false;
  
  if( starboardDistance == 0 || ( now - then ) > refresh_rate )
  {
    starboardDistance = raw_starboard_us( );
    then = now;
    *readStarboard = true;
  }
  
  return starboardDistance;
}
